package controllers
import (
	//"github.com/astaxie/beego"
	"LiquidMeasurement/forms"
	//"LiquidMeasurement/lib"
//	"go-lang-idea-plugin/testData/mockSdk-1.1.2/src/pkg/fmt"
	//"fmt"
	m"LiquidMeasurement/models"
	"strconv"
//"github.com/astaxie/beego/validation"
	//"github.com/astaxie/beego/validation"
"LiquidMeasurement/lib"
)

type DepartmentController struct {
	AdminController
}

func (this *DepartmentController) CreateDepartment() {
	method := this.Ctx.Input.Method()
	UserID := this.Ctx.Input.Param(":UserID")
	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		if method == "POST" {       //判断请求方式
			var form forms.DepartmentManageForm//new(forms.DepartmentManageForm)
			//this.ParseForm(&mLoginType)
			Errors := make([]lib.ERROR, 0, 10)
			//lib.(&form)
			if err := this.ParseForm(&form); err == nil {
				if _, err_msg := form.CheckValid(); len(err_msg) != 0 {
					Errors = append(Errors, err_msg...)
				}else {
					department := m.Department{Id:form.Id, Name:form.Name, Detail:form.Detail}
					_, err := m.AddDepartment(&department)
					if (err == nil) {
						this.Ctx.Redirect(302, "/admin/show_departments_list/" + UserID)
						return
					}else {
						Errors = append(Errors, lib.ERROR{0, "数据库出错"})
					}
				}

			}else {
				Errors = append(Errors, lib.ERROR{0, "解析表单错误"})
			}


			this.Data["Errors"] = &Errors
			this.Data["admin"] = &admin

			this.TplName = "add_department.html"
			//this.Abort("401")
			return
		}else {
			this.Data["UserID"] = UserID
			this.Data["admin"] = &admin
			this.TplName = "add_department.html"
			return
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}
}


func (this *DepartmentController) DeleteDepartment() {
	UserID := this.Ctx.Input.Param(":UserID")
	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		method := this.Ctx.Input.Method()
		if method == "POST" {       //判断请求方式
			var form = new(forms.DepartmentManageForm)
			//this.ParseForm(&mLoginType)
			if err := this.ParseForm(form); err != nil {
				respone_json := forms.SiteResponeType{forms.SITE_PARA_ERROR, forms.INVALIDS, forms.INVALIDS}
				this.Data["json"] = &respone_json
				this.ServeJSON();
				return
			}
			var respone_json forms.SiteResponeType
			//department := m.Department{Id:form.Id,Name:form.Name,Detail:form.Detail}
			//		_,	err := m.DelDepartmentByID(form.Id)
			//fmt.Println("err:",err)
			if _, err := m.DelDepartmentByID(form.Id); err != nil {
				respone_json = forms.SiteResponeType{forms.SITE_OPERATION_ERROR, forms.INVALIDS, forms.INVALIDS}

			}else {
				respone_json = forms.SiteResponeType{forms.SITE_SUCCESS, forms.INVALIDS, forms.INVALIDS}
			}
			this.Data["json"] = &respone_json
			this.ServeJSON();
			//this.TplName = "show_departments.html"
			return
		}else {
			respone_json := forms.SiteResponeType{forms.SITE_METHOD_ERROR, "数据提交方式错误", forms.INVALIDS}
			this.Data["json"] = &respone_json
			this.ServeJSON();
			return
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}
}


func (this *DepartmentController) ShowDepartmentList() {
	UserID := this.Ctx.Input.Param(":UserID")

	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		departments, _ := m.GetDepartmentList()
		//	Errors,_ :=this.Ctx.Input.Param(":Name")
		this.Data["departments"] = &departments
		this.Data["UserID"] = UserID
		this.Data["admin"] = &admin

		//	this.Data["Errors"] = Errors
		this.TplName = "show_departments.html"
		return
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}
}


func (this *DepartmentController) UpdateDepartment() {
	method := this.Ctx.Input.Method()
	UserID := this.Ctx.Input.Param(":UserID")
	id := this.Ctx.Input.Param(":id")
	Id, _ := strconv.ParseUint(id, 10, 64)

	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		if method == "POST" {
			Errors := make([]lib.ERROR,0,10)
			var form = new(forms.DepartmentManageForm)
			//this.ParseForm(&mLoginType)
			if err := this.ParseForm(form); err == nil {
				if _,err_msg :=form.CheckValid();len(err_msg) != 0{
					Errors = append(Errors,err_msg...)
				}else{
					department := m.Department{Id:form.Id, Name:form.Name, Detail:form.Detail}
					_, err := m.UpdateDepartment(&department)

					if (err == nil) {
						//this.Data["department"] = &department
						//this.TplName = "update_department.html"
						this.Ctx.Redirect(302, "/admin/show_departments_list/" + UserID)
						return
					}else {
						Errors = append(Errors,lib.ERROR{0,"数据库更新错误"})

					}
				}
			}
			department, _ := m.GetDepartmnetById(Id)

			this.Data["department"] = &department
			this.Data["Errors"] = &Errors
			this.Data["admin"] = &admin

			this.TplName = "update_department.html"
			return

		}else {
			this.Data["UserID"] = UserID

			department, err := m.GetDepartmnetById(Id)
			if (err == nil) {
				this.Data["department"] = &department
				this.Data["admin"] = &admin

				this.TplName = "update_department.html"
				return
			}else {
				this.Abort("401")
				return
			}
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")

	}
}


